package com.opencee.common.model;


import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.opencee.common.constants.ResultCode;
import com.opencee.common.utils.LocaleUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.ToString;

import java.io.Serializable;
import java.util.Map;

/**
 * @author admin
 */
@Data
@ApiModel(value = "正常响应结果")
@JsonIgnoreProperties(ignoreUnknown = true)
@ToString
public class ApiResult<T> implements Serializable {
    private static final long serialVersionUID = -6190689122701100762L;

    /**
     * 编码
     */
    @ApiModelProperty(value = "编码:200-请求处理成功")
    private int code = ResultCode.OK.getCode();

    /**
     * 提示消息
     */
    @ApiModelProperty(value = "提示消息")
    private String message;

    /**
     * 响应数据
     */
    @ApiModelProperty(value = "响应数据")
    private T data;

    /**
     * 附加数据,非泛型(data)约定数据
     */
    @ApiModelProperty(value = "附加数据,非泛型(data)约定数据")
    private JSONObject extras = new JSONObject();


    /**
     * 服务器时间
     */
    @ApiModelProperty(value = "服务器时间")
    private long timestamp = System.currentTimeMillis();

    /**
     * 操作是否成功
     *
     * @return
     */
    @ApiModelProperty(value = "操作是否成功")
    @JSONField(name = "success", ordinal = 1)
    @JsonProperty(value = "success", index = 1)
    public boolean isOk() {
        return this.code == ResultCode.OK.getCode();
    }

    /**
     * 是否操作错误
     *
     * @return
     */
    @JSONField(serialize = false)
    @JsonIgnore
    public boolean isFailed() {
        return this.code != ResultCode.OK.getCode();
    }


    /**
     * 正常
     *
     * @return
     */
    public static ApiResult ok() {
        return new ApiResult().code(ResultCode.OK);
    }

    /**
     * 正常
     *
     * @param message
     * @return
     */
    public static ApiResult ok(String message) {
        return new ApiResult().code(ResultCode.OK.getCode()).msg(message);
    }

    /**
     * 错误
     *
     * @return
     */
    public static ApiResult failed() {
        return new ApiResult().code(ResultCode.FAILED);
    }

    public static ApiResult failed(String message) {
        return new ApiResult().code(ResultCode.FAILED.getCode()).msg(message);
    }

    public ApiResult code(int code) {
        this.code = code;
        return this;
    }

    public ApiResult code(ResultCode code) {
        this.code = code.getCode();
        this.msgI18n(code.getMessageCode());
        return this;
    }

    public ApiResult msg(String message) {
        this.message = message;
        return this;
    }

    /**
     * 消息国际化读取
     *
     * @param messageCode 消息code
     * @param args        参数 {0},{1}
     * @return
     */
    public ApiResult msgI18n(String messageCode, Object... args) {
        return msgI18n("com.opencee.common.i18n.messages", messageCode, args);
    }

    /**
     * 消息国际化读取
     *
     * @param basename    文件名
     * @param messageCode 消息code
     * @param args        参数 {0},{1}
     * @return
     */
    public ApiResult msgI18n(String basename, String messageCode, Object... args) {
        this.message = LocaleUtil.getMessage(basename, messageCode, args);
        return this;
    }

    public ApiResult data(T data) {
        this.data = data;
        return this;
    }

    public ApiResult put(String key, Object value) {
        if (this.extras == null) {
            this.extras = new JSONObject();
        }
        this.extras.put(key, value);
        return this;
    }

    public ApiResult putAll(Map<String, Object> map) {
        if (this.extras == null) {
            this.extras = new JSONObject();
        }
        this.extras.putAll(map);
        return this;
    }


}
